//
//  UIDevice+MAAdv.m
//  MateAdv
//
//  Created by 茶 on 2023/9/8.
//

#import "UIDevice+MAAdv.h"

#import <SAMKeychain.h>

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

#define kKeyChainService @"com.mateadv.lovegame"
#define kKeyChainAccount @"mateUUID"

#define kDefaultsIdfaKey @"MACatergories.UIDevice.idfa"

@implementation UIDevice (MAAdv)

- (BOOL)isPad{
    static dispatch_once_t one;
    static BOOL pad;
    dispatch_once(&one, ^{
        pad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
    });
    return pad;
}

+ (CGFloat)statusHeight {
    float statusBarHeight = 0;
    if (@available(iOS 13.0, *)) {
        UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
        statusBarHeight = statusBarManager.statusBarFrame.size.height;
    }
    else {
        statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    }
    return statusBarHeight;
}

+ (CGFloat)navHeight{
    return 44;
}

/*利用SAMKeyChain生成并保存唯一设备号*/
+ (NSString *)getDeviceNumber {
    // 首先获取钥匙串中的设备号
    NSString *deviceNumber = [SAMKeychain passwordForService:kKeyChainService account:kKeyChainAccount];
    if (0 == deviceNumber.length) {
        // 获取设别id
        deviceNumber = [[UIDevice currentDevice] identifierForVendor].UUIDString;
        // 将-去掉
        deviceNumber = [deviceNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
        [SAMKeychain setPassword:deviceNumber forService:kKeyChainService account:kKeyChainAccount];
    }
    return deviceNumber;
}

+ (void)ma_idfaWithResult:(void(^)(NSString *))result {
    static NSString *idfa = @"NULL";
    if (@available(iOS 14, *)) {
        ATTrackingManagerAuthorizationStatus status = ATTrackingManager.trackingAuthorizationStatus;
        switch (status) {
            case ATTrackingManagerAuthorizationStatusNotDetermined:
            {
                NSLog(@"用户未做选择或未弹窗");
                static NSString *idfa1 = @"NULL";
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                    if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                        idfa1 = [ASIdentifierManager.sharedManager advertisingIdentifier].UUIDString;
                        idfa = idfa1.length ? idfa1 : @"NULL";
                    }else{
                        NSLog(@"请在设置-隐私-Tracking中允许App请求跟踪");
                        idfa = idfa1.length ? idfa1 : @"NULL";
                        [kUserDefaults setObject:idfa forKey:kDefaultsIdfaKey];
                    }
                    MABlock(result,idfa);
                }];
            }
                break;
            case ATTrackingManagerAuthorizationStatusRestricted:
                //用户在系统级别开启了限制广告追踪
                NSLog(@"请在设置-隐私-Tracking中允许App请求跟踪");
                idfa= @"NULL";
                MABlock(result,idfa);
                break;
            case ATTrackingManagerAuthorizationStatusDenied:
                NSLog(@"用户拒绝");
                idfa= @"NULL";
                MABlock(result,idfa);
                break;
            case ATTrackingManagerAuthorizationStatusAuthorized:
                NSLog(@"用户允许");
                static NSString *idfa2 = @"NULL";
                idfa2 = [ASIdentifierManager.sharedManager advertisingIdentifier].UUIDString;
                idfa = idfa2.length ? idfa2 : @"NULL";
                [kUserDefaults setObject:idfa forKey:kDefaultsIdfaKey];
                MABlock(result,idfa);
                break;
        }
        
    }else{
        static NSString *staticIdfa;
        if (!staticIdfa) {
            staticIdfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        }
        idfa = staticIdfa;
        
        idfa = idfa.length ? idfa : @"NULL";
        [kUserDefaults setObject:idfa forKey:kDefaultsIdfaKey];
        MABlock(result,idfa);
    }
}

@end
